﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Script.Serialization;

using Axis.Web.Administrator.Global;
using Axis.BusinessObject.Core;
using Axis.Dao.Core;
using System.Web.SessionState;
namespace Axis.Web.Administrator.Views.Accounts.Service
{
    /// <summary>
    /// Summary description for UserService
    /// </summary>
    public class UserService : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["Action"];
            string output = string.Empty;
            switch (action)
            {
                case "INSERT":
                case "UPDATE":
                case "DELETE":
                    output = UpdateUser(context);
                    break;
                default:
                    output = GetUserList(context);
                    break;
            }

            context.Response.Write(output);
        }
        protected string UpdateUser(HttpContext context)
        {
            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);
            /* Action */
            CCoreDao core = new CCoreDao();
            CApplicationMessage message = core.ActionUserList(inputValue);
            string output = new JavaScriptSerializer().Serialize(message);
            return output;
        }
        protected string GetUserList(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];
            

            /* Get QueryString to xml */
            CCore cCore = new CCore();
            string inputValue =  cCore.GetInputValue(context);
            CCoreDao core = new CCoreDao();
            IList<CUserList> list = core.GetUserList(inputValue);
            int totalRecords = core.LastOutputValue.TotalRows;
            int totalPages = core.LastOutputValue.TotalPages;
            string output = BuildJQGridResults(list, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), totalPages);
            return output;
        }
        protected string BuildJQGridResults(IList<CUserList> users, int numberOfRows, int pageIndex, int totalPages)
        {
            if (users != null && users.Count > 0)
            {
                CGrid.JQGridResults result = new CGrid.JQGridResults();
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CUserList user in users)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = user.ID;
                    row.cell = new string[13];
                    
                    row.cell[0] = user.ID.ToString();
                    row.cell[1] = user.FullName;
                    row.cell[2] = user.LoginName;
                    row.cell[3] = user.Email;
                    row.cell[4] = user.Mobile;
                    row.cell[5] = user.Phone;
                    string dateOfBirth = user.DateOfBirth.ToString();
                    if (dateOfBirth.Split(' ').Length > 0)
                    {
                        dateOfBirth = dateOfBirth.Split(' ')[0];
                    }
                    row.cell[6] = dateOfBirth;
                    row.cell[7] = user.GenderName;
                    row.cell[8] = user.Gender.ToString();
                    row.cell[9] = user.Address;
                    row.cell[10] = user.Ward;
                    row.cell[11] = user.ProvinceName;
                    row.cell[12] = user.ProvinceID.ToString();
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
                return new JavaScriptSerializer().Serialize(result);
            }
            else
            {
                return string.Empty;
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}